﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class Collet
    {
        public static void ColletWithStraightShoulder(Dictionary<string, Object> data)    //直肩夹头
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            string path = Convert.ToString(data["path"]);
            double s = Convert.ToDouble(data["s"]);
            double pi = Math.PI;
            double an = Convert.ToDouble(data["an"]) * pi / 180;
            double lbb = Convert.ToDouble(data["lbb"]);
            double w1 = Convert.ToDouble(data["w1"]);
            double offset = 10;
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double l = Convert.ToDouble(data["l"]);
            double le = Convert.ToDouble(data["le"]);
            double ann = Convert.ToDouble(data["ann"]) * pi / 180;
            double re = Convert.ToDouble(data["re"]) * pi / 180;
            //double ann = 15 * pi / 180;
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(-Math.Tan(an) * s, 0);
            Vector2 v3 = new Vector2(-w1 + Math.Tan(an) * s, 0);
            Vector2 v4 = new Vector2(-w1, s);
            Vector2 v5 = new Vector2(-lbb, v4.Y);
            Vector2 v6 = new Vector2(-(lbb - s * Math.Tan(pi / 6)), v5.Y - Math.Tan(pi / 6) * (lbb - s * Math.Tan(pi / 6)));
            Vector2 v7 = new Vector2(v1.X, v5.Y);
            //
            Vector2 v8 = new Vector2(v1.X, v1.Y - 20);
            Vector2 v9 = new Vector2(v8.X, v8.Y - l + (Math.Cos(pi / 6) * 2) * 2);
            Vector2 v10 = new Vector2(v9.X - Math.Sin(pi / 6) * 2, v9.Y - Math.Cos(pi / 6));
            Vector2 v11 = new Vector2(-w1 + Math.Sin(pi / 6) + Math.Tan(epsr) * (Math.Sin(pi / 6) / Math.Tan(epsr)), v10.Y + Math.Tan(pi / 2 - epsr) * (w1 - Math.Sin(pi / 6) * 2));
            Vector2 v12 = new Vector2(-w1 + Math.Sin(pi / 6), v11.Y + (Math.Sin(pi / 6) / Math.Tan(epsr)) * 4);
            Vector2 v13 = new Vector2(v12.X, v12.Y + l - (Math.Cos(pi / 6) * 2) * 2);
            Vector2 v14 = new Vector2(v12.X + Math.Sin(pi / 3) * 2, v13.Y + Math.Cos(pi / 6));
            Vector2 v15 = new Vector2(v10.X, v14.Y - Math.Tan(pi / 2 - epsr) * (w1 - Math.Sin(pi / 3) * 4));
            Vector2 v16 = new Vector2(v9.X, v15.Y - (Math.Sin(pi / 3) / Math.Tan(epsr)) * 4);
            Vector2 v17 = new Vector2(v14.X, v11.Y + le + v14.X * Math.Tan(pi / 2 - epsr));
            Vector2 v18 = new Vector2(v12.X, v13.Y - le + Math.Cos(pi / 6) * 3);
            Vector2 v19 = new Vector2(v9.X, v9.Y + le);
            Vector2 v20 = new Vector2(v9.X - Math.Sin(pi / 6) * 3, v10.Y + le);
            Vector2 v21 = new Vector2(v20.X, v10.Y + Math.Cos(pi / 6) / 2);
            //
            Vector2 v22 = new Vector2(v11.X - 10, v12.Y + 10 * Math.Tan(pi / 2 - epsr));
            Vector2 v23 = new Vector2(v22.X + 10 * Math.Tan(pi / 2 - ann - epsr) / 6 / Math.Tan(ann), v22.Y - 10 * Math.Tan(pi / 2 - ann - epsr) / 6);
            Vector2 v24 = new Vector2(v22.X + 10 * Math.Tan(pi / 2 - ann - epsr) / 4 / Math.Tan(ann) / 2, v22.Y + 10 * Math.Tan(pi / 2 - ann - epsr) / 4 / Math.Tan(ann) / 2 * Math.Tan(epsr));
            Vector2 v25 = new Vector2(v23.X + Math.Tan(pi / 2 - epsr) * 10 * Math.Tan(pi / 2 - ann - epsr) / 4 / Math.Tan(ann) / 2, v22.Y + 10 * Math.Tan(pi / 2 - ann - epsr) / 4 / Math.Tan(ann) / 2);
            // 线段
            //主视图
            Vector2[] Line1 = { v3, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v3, v4 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v4, v5 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            LwPolyline Arc1 = new LwPolyline();
            Arc1.Vertexes.Add(new LwPolylineVertex(v5));
            Arc1.Vertexes.Add(new LwPolylineVertex(v6));
            Arc1.Vertexes[0].Bulge = Math.Tan(an) * (lbb + s * Math.Tan(pi / 6)) / 2;
            fun.PolyFill(dxf, Arc1, 0.3, contour);
            Vector2[] Line4 = { v6, v7 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v2, v7 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            //
            LwPolyline Arc2 = new LwPolyline();
            Arc2.Vertexes.Add(new LwPolylineVertex(v9));
            Arc2.Vertexes.Add(new LwPolylineVertex(v10));
            Arc2.Vertexes[0].Bulge = -Math.Sin(pi / 6);
            fun.PolyFill(dxf, Arc2, 0.3, contour);
            Vector2[] Line6 = { v10, v11 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(v12));
            Arc3.Vertexes.Add(new LwPolylineVertex(v11));
            Arc3.Vertexes[0].Bulge = (Math.Sin(pi / 6) / Math.Tan(epsr) / Math.Cos(epsr) * Math.Sin(epsr)) / 2;
            fun.PolyFill(dxf, Arc3, 0.3, contour);
            Vector2[] Line7 = { v12, v13 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            LwPolyline Arc4 = new LwPolyline();
            Arc4.Vertexes.Add(new LwPolylineVertex(v13));
            Arc4.Vertexes.Add(new LwPolylineVertex(v14));
            Arc4.Vertexes[0].Bulge = -Math.Sin(pi / 6);
            fun.PolyFill(dxf, Arc4, 0.3, contour);
            Vector2[] Line8 = { v14, v15 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            LwPolyline Arc5 = new LwPolyline();
            Arc5.Vertexes.Add(new LwPolylineVertex(v15));
            Arc5.Vertexes.Add(new LwPolylineVertex(v16));
            Arc5.Vertexes[0].Bulge = -(Math.Sin(pi / 6) / Math.Tan(epsr) / Math.Cos(epsr) * Math.Sin(epsr)) / 2;
            fun.PolyFill(dxf, Arc5, 0.3, contour);
            Vector2[] Line9 = { v16, v9 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Line line10 = new Line(v7, new Vector2(v7.X, v2.Y));
            line10.Layer = dimensioning;
            Vector2[] Line11 = { v17, v18 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { v14, v17 };
            fun.drawBLine(dxf, Line12, 0.3, contour);
            Vector2[] Line13 = { v19, v20 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { v20, v21 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            //
            Vector2[] Line15 = { v11, v22 };
            fun.drawBLine(dxf, Line15, 0.3, contour);
            Vector2[] Line16 = { v22, v23 };
            fun.drawBLine(dxf, Line16, 0.3, contour);
            Vector2[] Line17 = { v22, v24 };
            fun.drawBLine(dxf, Line17, 0.3, contour);
            Vector2[] Line18 = { v25, v23 };
            fun.drawBLine(dxf, Line18, 0.3, contour);
            
            Vector2[] Line19 = { v24, v25 };
            fun.drawPlinetype(dxf, Line19, 0.15, centre, Linetype.Center);
            
            //Vector2[] Line20 = {v24,v25};
            //fun.drawPlinetype(dxf, Line20, refin, Linetype.Dashed);
            //尺寸标注
            DimensionStyle ds = new DimensionStyle("sd");
            ds.ArrowSize = 1.6;
            ds.DimArrow1 = DimensionArrowhead.Closedfilled;
            ds.DimArrow2 = DimensionArrowhead.Closedfilled;
            ds.TextHeight = 2.0;
            Angular2LineDimension dimang1 = new Angular2LineDimension(new Line(v7, v2), new Line(v7, new Vector2(v7.X, v2.Y)), offset * 1.5, ds);
            dimang1.UserText = String.Format("AN={0}°", an * 180 / pi);
            dxf.AddEntity(dimang1);

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
